package com.google.code.ptrends.Application.DAL.entities;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Embedded;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Transient;

@Entity
@Table(name = "APP.ITEMS")
public class ItemEntity implements Serializable {

	private static final long serialVersionUID = 1L;

	private int id;
	private ClassesEntity classes;
	private CategoryEntity category;
	private ManufacturerEntity manufacturer;
	private SupplierEntity supplier;
	private String model;
	private Date versionDate;
	private List<ItemParameterEntity> parameters = new ArrayList<ItemParameterEntity>();
	@Transient
	private String params;

	@Id
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	@ManyToOne
	@Embedded
	@JoinColumn(name = "CLASS_ID", nullable = false)
	public ClassesEntity getClasses() {
		return classes;
	}

	public void setClasses(ClassesEntity classes) {
		this.classes = classes;
	}

	@ManyToOne
	@Embedded
	@JoinColumn(name = "CATEGORY_ID", nullable = false)
	public CategoryEntity getCategory() {
		return category;
	}

	public void setCategory(CategoryEntity category) {
		this.category = category;
	}

	@ManyToOne
	@Embedded
	@JoinColumn(name = "MANUFACTURER_ID", nullable = false)
	public ManufacturerEntity getManufacturer() {
		return manufacturer;
	}

	public void setManufacturer(ManufacturerEntity manufacturer) {
		this.manufacturer = manufacturer;
	}

	@Column(name = "MODEL", nullable = false, length = 128)
	public String getModel() {
		return model;
	}

	public void setModel(String model) {
		this.model = model;
	}

	@Column(name = "VERSION_DATE", nullable = false)
	public Date getVersionDate() {
		Date cloned = null;
		if (versionDate != null) {
			cloned = new Date(versionDate.getTime());
		}
		return cloned;
	}

	public void setVersionDate(Date versionDate) {
		this.versionDate = versionDate == null ? null : new Date(
				versionDate.getTime());
	}

	public void setParameters(List<ItemParameterEntity> parameters) {
		this.parameters = parameters;
	}

	@OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
	@JoinTable(name = "ITEM_VALUES", joinColumns = { @JoinColumn(name = "ITEM_ID", nullable = false, updatable = false) }, inverseJoinColumns = { @JoinColumn(name = "ID", nullable = false, updatable = false) })
	public List<ItemParameterEntity> getParameters() {
		return parameters;
	}

	public void setParams(String params) {
		this.params = params;
	}

	@Transient
	public String getParams() {
		StringBuilder sb = new StringBuilder();
		ItemParameterEntity[] p = this.parameters
				.toArray(new ItemParameterEntity[this.parameters.size()]);
		for (int i = 0; i < p.length; i++) {
			sb.append(p[i].getValue());
			if (i != p.length - 1)
				sb.append(" / ");
		}
		this.params = sb.toString();
		return params;
	}

	public void setSupplier(SupplierEntity supplier) {
		this.supplier = supplier;
	}

	@ManyToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
	@JoinTable(name = "PRICES", joinColumns = { @JoinColumn(name = "ITEM_ID", nullable = false, updatable = false) }, inverseJoinColumns = { @JoinColumn(name = "SUPPLIER_ID", nullable = false, updatable = false) })
	public SupplierEntity getSupplier() {
		return supplier;
	}

}
